package com.klhappy.houyuan.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.activity.RatingActivity;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.utils.imageselector.Bimp;
import com.klhappy.houyuan.utils.imageselector.ImageItem;
import com.klhappy.houyuan.view.banner.DisplayUtil;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

/**
 * Created by MrPu on 16/1/10.
 */
public class GridAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private int selectedPosition = -1;
    private boolean shape;
    int creenW, creenH;
    private List<ImageItem> lists;
    private Context context;
    public Handler handler;

    public boolean isShape() {
        return shape;
    }

    public void setShape(boolean shape) {
        this.shape = shape;
    }

    public GridAdapter(Context context) {
        this.context = context;
    }

    public GridAdapter(Context context, DisplayMetrics dm, List<ImageItem> lists) {
        this.context = context;
        this.creenW = dm.widthPixels;
        this.creenH = dm.heightPixels;
        this.lists = lists;
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        if (lists.size() == 4) {
            return 4;
        }
        return (lists.size() + 1);
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int arg0) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_rating_img, parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.item_grida_image);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_qingchu);
            ViewGroup.LayoutParams layoutParams = holder.image.getLayoutParams();
            layoutParams.height = creenW / 10;
            layoutParams.width = creenW / 10;
            holder.image.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == lists.size()) {
            holder.image.setImageBitmap(BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.add_picture_img));
            holder.imageView.setVisibility(View.GONE);
            if (position == 4) {
                holder.image.setVisibility(View.GONE);
            }
        } else {
            holder.imageView.setVisibility(View.VISIBLE);
            int size = DisplayUtil.getWindowWidth(context) / 8;
            Picasso.with(context)
                    .load(new File(lists.get(position).getImagePath()))
                    .placeholder(R.drawable.default_img)
                    .error(R.drawable.default_img)
                    .centerCrop()
                    .resize(size, size)
                    .into(holder.image);
        }

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lists.remove(position);
                NotifyMethod.getInstance().postNotification(RatingActivity.REMOVE, position);
            }
        });

        return convertView;
    }

    public class ViewHolder {
        public ImageView image;
        public ImageView imageView;
    }
}